﻿using Dyao.Book.Entity;
using Dyao.Book.Repository.Abstractions;
using System.Linq.Expressions;

namespace Dyao.Book.Repository
{
    using Foundation.Replacement;

    public class BookshelfRepository : DataRepository<BookshelfEntity>, IBookshelfRepository
    {
        /// <inheritdoc cref="IBookshelfRepository.GetListByUserIdAsync"/>
        public async Task<List<T>> GetListByUserIdAsync<T>(string userId, Expression<Func<BookshelfEntity, T>> selector, CancellationToken cancellationToken)
        {
            var query = this.GetQueryable()
                .Where(bs => bs.UserId == userId)
                .OrderByDefault(bs => bs.LastReadingDateTime)
                .Select(selector);

            return await query.ToListAsync();
        }

        /// <inheritdoc cref="IBookshelfRepository.GetListByUserIdAsync"/>
        public async Task<BookshelfEntity> GetByUserIdAsync(string userId, string bookId, CancellationToken cancellationToken)
        {
            var query = this.GetQueryable()
                .Where(bs => bs.UserId == userId)
                .Where(bs => bs.BookId == bookId);

            return await query.FirstOrDefaultAsync(cancellationToken);
        }
    }
}
